using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FanDongping.Stock
{
    public partial class FmMain : Form
    {
        public FmMain()
        {
            InitializeComponent();
        }

        private void FmMain_Load(object sender, EventArgs e)
        {
            browse_Click(this, new EventArgs());
        }

        private void addRecord_Click(object sender, EventArgs e)
        {
            FmEditTrade fmAddRecord = GetAndActiveMDIForm(typeof(FmEditTrade)) as FmEditTrade; ;
        }

        private void save_Click(object sender, EventArgs e)
        {
            StockData.Save();
        }

        private void browse_Click(object sender, EventArgs e)
        {
            FmBrowse fb = GetAndActiveMDIForm(typeof(FmBrowse)) as FmBrowse;
        }

        private Form GetAndActiveMDIForm(Type formType)
        {
            Form fb = GetMdiForm(formType);

            if (fb == null)
            {
                if (formType == typeof(FmBrowse))
                {
                    fb = new FmBrowse();
                }
                else if (formType == typeof(FmEditTrade))
                {
                    fb = new FmEditTrade();
                }
                else if (formType == typeof(FmViewStockInfos))
                {
                    fb = new FmViewStockInfos();
                }
                else if (formType == typeof(FmTradeConfig))
                {
                    fb = new FmTradeConfig();
                }
            }

            fb.MdiParent = this;
            fb.Show();
            fb.Activate();
            return fb;
        }

        private Form GetMdiForm(Type formType)
        {
            Form fb = null;

            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType() == formType)
                {
                    fb = f;
                    break;
                }
            }
            return fb;
        }

        public void CloseMdiForm(Type formType)
        {
            Form fb = GetMdiForm(formType);
            if (fb != null)
            {
                fb.Close();
            }
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FmTmp ft = new FmTmp();
            ft.MdiParent = this;
            ft.Show();
        }

        private void btnStockView_Click(object sender, EventArgs e)
        {
            GetAndActiveMDIForm(typeof(FmViewStockInfos));
        }

        private void FmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (StockData.Modified)
            {
                DialogResult d = MessageBox.Show("The data modified ,Save it?", "Save", MessageBoxButtons.YesNo);
                if (d == DialogResult.Yes)
                {
                    StockData.Save();
                }
            }
        }

        private void btntradeConfig_Click(object sender, EventArgs e)
        {
            GetAndActiveMDIForm(typeof(FmTradeConfig));
        }
    }
}